package com.longdaji.decoration.ui.payOrder;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.PayBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.RxUtil;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class PayOrderPresenter extends RxPresenter<PayOrderContract.View> implements PayOrderContract.Presenter {
    private final static String TAG = PayOrderPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public PayOrderPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getPayOrderInfo(String orderId, double money) {
        Map<String, Object> options = new HashMap<>();
        options.put("body", "桃心商品支付");
        options.put("subject", "桃心");
        options.put("out_trade_no", orderId);
        options.put("timeout_express", "30m");
        options.put("total_amount", money);
        addSubscribe(mDataManager.getPayOrderInfo(options)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<PayBean>handleResult())
                .subscribeWith(new CommonSubscriber<PayBean>() {
                    @Override
                    public void onSuccess(PayBean payBean) {
                        Log.d(TAG, "orderInfo =" + payBean.getOrderInfo());
                        if (mView != null) {
                            mView.showGetOrderInfoSuccess(payBean.getOrderInfo());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showGetOrderInfoFail();
                        }
                    }
                }));
    }

}
